﻿using FaceWebServer.Interface;
using FaceWebServer.Model;
using FaceWebServer.Utility;
using FaceWebServer.Utility.FilterAttribute;
using FaceWebServer.Utility.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace FC8280TServer.Controllers.FaceDevice
{
    [Route("api/[controller]")]
    [ApiController]
    public class DeviceReportController : ControllerBase
    {

        private readonly ILogger<DeviceReportController> _logger;
        private IMyMemoryCache _Cache;
        private IRecordService _RecordDB;

        public DeviceReportController(ILogger<DeviceReportController> logger,
            IRecordService door, IMyMemoryCache mycache)
        {
            _logger = logger;
            _RecordDB = door;
            _Cache = mycache;
        }


        /// <summary>
        /// 获取设备出入记录
        /// </summary>
        /// <param name="par"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("GetReport")]
        [Authorize]
        [TypeFilter(typeof(VerifyActionFilterAttribute))]
        public IActionResult GetReport([FromForm] DeviceReportQueryParameter par)
        {

            List<Expression<Func<DriveRecord, bool>>> oWheres = new List<Expression<Func<DriveRecord, bool>>>();
            oWheres.Add(x => x.noteTime >= par.ReportBegin && x.noteTime <= par.ReportEnd);

            if (!string.IsNullOrWhiteSpace(par.DeviceID)) oWheres.Add(x => x.deviceId.Contains(par.DeviceID));
            if (par.EmployeeID > 0) oWheres.Add(x => x.employeeId == par.EmployeeID);
            if (par.eventType > 0) oWheres.Add(x => x.eventType == par.eventType);

            _logger.LogInformation("获取出入记录列表");
            var reports = _RecordDB.QueryPage(
                oWheres, par.pageSize, par.pageIndex,
                x => x.noteTime,
                par.isAsc);

            return new JsonResult(new JsonResultModel(reports));
        }



    }
}
